/*
 * Copyright (c) create by xjs 16-9-29 下午6:56
 */

package com.example.commonlib.commonutil;

import com.google.gson.Gson;

/**
 * Created by xjs on 2016/9/29.
 */
public class GsonTool {
    private static Gson gson = null;

    static {
        if (gson == null) {
            synchronized (Gson.class) {
                if (gson == null) {
                    gson = new Gson();
                }
            }
        }
    }

    public static Gson getGson() {
        return gson;
    }

    private GsonTool() {
    }

    public static String beanTojson(Object c) {
        try {
            return gson.toJson(c);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static <T> T jsonToBean(String json, Class<T> c) {
        try {
            return gson.fromJson(json, c);
        } catch (Exception e) {
            e.printStackTrace();
            MLog.e("gson fail");
            return null;
        }
    }

}
